package com.danger.Activity;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.ColorMatrix;
import android.graphics.ColorMatrixColorFilter;
import android.graphics.Paint;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.GridLayout;
import android.widget.GridView;
import android.widget.ImageView;

public class ColorMatricActivity extends AppCompatActivity implements View.OnClickListener {

    private ImageView imageView;
    private GridLayout gridLayout;
    private Button change_button;
    private Button reset_button;
    private Bitmap bitmap;
    private int editWidth;
    private int editHeight;
    private EditText[] editTexts = new EditText[20];
    private float[] colorMatrixs = new float[20];

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_color_matric);

        initView();
        initEvent();

    }

    private void initView() {
        bitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.test1);
        imageView = (ImageView) findViewById(R.id.imageView);
        gridLayout = (GridLayout) findViewById(R.id.gridLayout);
        change_button = (Button) findViewById(R.id.change_button);
        reset_button = (Button) findViewById(R.id.reset_button);

        imageView.setImageBitmap(bitmap);

        //在onCreate方法中不能获取到控件的宽高，因为控件可能还没绘制完，因此，要使用view.post方法，该方法会等到控件绘制完之后执行
        gridLayout.post(new Runnable() {
            @Override
            public void run() {
                editWidth = gridLayout.getWidth() / 5;
                editHeight = gridLayout.getHeight() / 4;
                addEditText();
                initMatrix();
            }
        });
    }

    private void initEvent() {
        change_button.setOnClickListener(this);
        reset_button.setOnClickListener(this);
    }

    private void addEditText() {
        for (int i = 0; i < 20; i++) {
            EditText editText = new EditText(this);
            editTexts[i] = editText;
            gridLayout.addView(editText, editWidth, editHeight);
        }
    }

    private void initMatrix() {
        for (int i = 0; i < 20; i++) {
            if (i % 6 == 0) {
                editTexts[i].setText(String.valueOf(1));
            } else {
                editTexts[i].setText(String.valueOf(0));
            }
        }
    }

    private void setImageMatrix() {
        Bitmap bmp = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888);
        ColorMatrix colorMatrix = new ColorMatrix();
        colorMatrix.set(colorMatrixs);
        Canvas canvas = new Canvas(bmp);
        Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        paint.setColorFilter(new ColorMatrixColorFilter(colorMatrix));
        canvas.drawBitmap(bitmap, 0, 0, paint);
        imageView.setImageBitmap(bmp);
    }

    private void getMatrix() {
        for (int i = 0; i < 20; i++) {
            colorMatrixs[i] = Float.parseFloat(editTexts[i].getText().toString());
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.change_button:
                getMatrix();
                setImageMatrix();
                break;
            case R.id.reset_button:
                initMatrix();
                getMatrix();
                setImageMatrix();
                break;
            default:
                break;
        }
    }
}
